<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: action.group.php Sat Jul 18 13:22:28 CST 2009
 */

include "header.php";

$groups_handler = xoops_getmodulehandler("groups","groups");

$op = isset($_GET["op"]) ? trim($_GET["op"]) : "";
$ac = isset($_POST["ac"]) ? trim($_POST["ac"]) : "";

$group_id = isset($_REQUEST["group_id"]) ? intval($_REQUEST["group_id"]) : 0;

if ( empty($group_id) ) redirect_header("groups.php");
$group_obj = $groups_handler->get($group_id);

if ( !is_object($group_obj) ) redirect_header("groups.php");

switch ($op) {
	case "cancel":
		switch ($ac) {
			default:
				xoops_confirm(array('group_id' => $group_id, 'ac' => "save"), 'action.group.php?op=cancel', "确定取消 [".$group_obj->getVar("group_name")."] 圈子的权限");
			break;
			
			case "save":
				if (!$GLOBALS['xoopsSecurity']->check()) {
		            redirect_header("groups.php", 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
		            exit();
		        }
				$group_obj->setVar("group_status","0");
				if ( $groups_handler->insert($group_obj) ) {
					$stop = "操作功能";
				} else {
					$stop = "操作失败";
				}
				redirect_header("groups.php",5,$stop);
			break;
		}
	break;
	case "pass":
		switch ($ac) {
			default:
				xoops_confirm(array('group_id' => $group_id, 'ac' => "save"), 'action.group.php?op=pass', "确定通过 [".$group_obj->getVar("group_name")."] 圈子的审核");
			break;
			
			case "save":
				if (!$GLOBALS['xoopsSecurity']->check()) {
		            redirect_header("groups.php", 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
		            exit();
		        }
				$group_obj->setVar("group_status","1");
				if ( $groups_handler->insert($group_obj) ) {
					$stop = "操作功能";
				} else {
					$stop = "操作失败";
				}
				redirect_header("groups.php",5,$stop);
			break;
		}
	break;
	case "off":
		switch ($ac) {
			default:
				xoops_confirm(array('group_id' => $group_id, 'ac' => "save"), 'action.group.php?op=off', "确定禁止 [".$group_obj->getVar("group_name")."] 圈子的访问");
			break;
			
			case "save":
				if (!$GLOBALS['xoopsSecurity']->check()) {
		            redirect_header("groups.php", 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
		            exit();
		        }
				$group_obj->setVar("group_status","2");
				if ( $groups_handler->insert($group_obj) ) {
					$stop = "操作功能";
				} else {
					$stop = "操作失败";
				}
				redirect_header("groups.php",5,$stop);
			break;
		}
	break;
	case "delete":
		switch ($ac) {
			default:
				xoops_confirm(array('group_id' => $group_id, 'ac' => "save"), 'action.group.php?op=delete', "确定删除 [".$group_obj->getVar("group_name")."] 圈子, 该圈子所有文章组ID=0， 该组成员、管理员清空");
			break;
			
			case "save":
				if (!$GLOBALS['xoopsSecurity']->check()) {
		            redirect_header("groups.php", 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
		            exit();
		        }
				if ( $groups_handler->delete($group_obj) ) {
					$threads_handler = xoops_getmodulehandler("threads","groups");
					$posts_handler = xoops_getmodulehandler("posts","groups");
					$members_handler = xoops_getmodulehandler("members","groups");
					$groupadmin_handler = xoops_getmodulehandler("groupadmin","groups");
					$criteria = new CriteriaCompo(new Criteria("group_id",$group_id));
					$threads_handler->updateAll("group_id","0",$criteria);
					$posts_handler->updateAll("group_id","0",$criteria);
					$members_handler->deleteAll($criteria);
					$groupadmin_handler->deleteAll($criteria);
					$stop = "操作功能";
				} else {
					$stop = "操作失败";
				}
				redirect_header("groups.php",5,$stop);
			break;
		}
	break;
	case "empty":
		switch ($ac) {
			default:
				xoops_confirm(array('group_id' => $group_id, 'ac' => "save"), 'action.group.php?op=empty', "确定清空 [".$group_obj->getVar("group_name")."] 圈子的所有文章.");
			break;
			
			case "save":
				if (!$GLOBALS['xoopsSecurity']->check()) {
		            redirect_header("groups.php", 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
		            exit();
		        }
				$threads_handler = xoops_getmodulehandler("threads","groups");
				$posts_handler = xoops_getmodulehandler("posts","groups");
				$criteria = new CriteriaCompo(new Criteria("group_id",$group_id));
				
				$group_obj->setVar("group_topics","0");
				$group_obj->setVar("group_posts","0");
				$groups_handler->insert($group_obj);
				
				if ( $threads_handler->getCount($criteria) ) {
					$threads_handler->deleteAll($criteria);
					$posts_handler->deleteAll($criteria);
					$stop = "操作功能";
				} else {
					$stop = "操作失败,没有主题";
				}
				redirect_header("groups.php",5,$stop);
			break;
		}
	break;
	
}

include "footer.php";
?>